package com.djbx.points.member.controller;

import com.djbx.points.common.core.utils.ResponseCode;
import com.djbx.points.common.core.utils.ServiceResponse;
import com.djbx.points.common.core.utils.lock.ILock;
import com.djbx.points.common.core.utils.lock.impl.SimpleLock;
import com.djbx.points.member.api.dto.MemberInDTO;
import com.djbx.points.member.api.dto.MemberOutDTO;
import com.djbx.points.member.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 会员
 *
 * @author aliuql
 * @email 17701057157@qq.com
 * @date 2021-11-15 14:30:28
 */
@Slf4j
@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    @Autowired
    private RedissonClient redissonClient;

    /**
     * 获取明细
     */
    @GetMapping("/get")
    public ServiceResponse<MemberOutDTO> get(@RequestParam("id")Long id) {
        return memberService.getMember(id);
    }

    /**
     * 获取明细列表
     */
    @GetMapping("/getList")
    public ServiceResponse<MemberOutDTO> getList(@RequestBody MemberInDTO memberInDTO) {
//        CacheChannel channel = J2Cache.getChannel();
//        channel.set("Caches.Member", "aaaa", "bbbbb");

        return memberService.getList(memberInDTO);
    }

    /**
     * 获取明细列表(分页)
     */
    @GetMapping("/getPage")
    public ServiceResponse<MemberOutDTO> getPage(@RequestBody MemberInDTO memberInDTO) {
        return memberService.getPage(memberInDTO);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public ServiceResponse<Void> save(@RequestBody MemberInDTO memberInDTO) {
        String lockKey = memberInDTO.getAccessToken();
        ILock lock = SimpleLock.getLock(redissonClient, lockKey);
        ServiceResponse<Void> result = null;
        if (lock != null) {
            result = lock.tryLock(()->{
                return memberService.save(memberInDTO);
            });
//            result = lock.lock(()->{
//                return memberService.save(memberInDTO);
//            });
        }
        return result;
    }

    /**
     * 更新
     */
    @PostMapping("/update")
    public ServiceResponse<Void> update(@RequestBody MemberInDTO memberInDTO) {
        if(Objects.isNull(memberInDTO) || Objects.isNull(memberInDTO.getId())){
            return new ServiceResponse<>(ResponseCode.MISSING_REQUEST_PARAM_ERROR);
        }
        String lockKey = memberInDTO.getAccessToken();
        ILock lock = SimpleLock.getLock(redissonClient, lockKey);
        ServiceResponse<Void> result = null;
        if (lock != null) {
            result = lock.lock(()->{
                return memberService.update(memberInDTO);
            });
        }
        return result;
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public ServiceResponse<Void> delete(@RequestParam("id") Long id) {
        if(Objects.isNull(id)){
            return new ServiceResponse<>(ResponseCode.MISSING_REQUEST_PARAM_ERROR);
        }
        ILock lock = SimpleLock.getLock(redissonClient, id.toString());
        ServiceResponse<Void> result = null;
        if (lock != null) {
            result = lock.lock(()->{
                return memberService.delete(id);
            });
        }
        return result;
    }

}
